package br.com.chat.server.rmi.impl;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Collections;
import java.util.List;

import br.com.chat.Message;
import br.com.chat.User;
import br.com.chat.rmi.ChatManager;
import br.com.chat.rmi.MessageReceiver;
import br.com.chat.server.RemoteImpl;

@RemoteImpl("receiver")
public class MessageReceiverImpl extends UnicastRemoteObject implements MessageReceiver{

    private static final long serialVersionUID = 1L;
    
    public MessageReceiverImpl() throws RemoteException {
        super();
    }

    @Override
    public List<Message> receive(User user) throws RemoteException {
        ChatManager manager = ChatManagerImpl.getInstance();
        
        if ( !manager.isLoggedIn(user)){
            return Collections.emptyList();
        }
        
        return manager.load(user);
    }

}
